/*
leetcode 349
输入：nums1 = [1,2,2,1], nums2 = [2,2]
输出：[2]
*/
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        sort(nums1.begin(),nums1.end());
        sort(nums2.begin(),nums2.end());
        int len1=nums1.size(),len2=nums2.size();
        int index1=0,index2=0;
        vector<int> ans;
        while(index1<len1&&index2<len2){
        	int num1=nums1[index1],num2=nums2[index2];
        	if(num1==num2){
                //num1!=ans.back() 就保证了只会输入一个数字，而不会输入两个一样的数字
        		if(!ans.size()||num1!=ans.back()){
        			ans.push_back(num1);
				}
				index1++;
				index2++;
			}else if(num1<num2){
				index1++;
			}else{
				index2++;
			}
		}
		return ans;
    }
};